﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;


namespace DDD4Fun.Ducks
{
    public class Flock : IQuackable
    {
        IList<IQuackable> ducks = new List<IQuackable>();
     
        public void Add(IQuackable duck)
        {
            ducks.Add(duck);
        }

        #region IQuackable Members

        public void Quack()
        {
            ////foreach版本
            //Console.WriteLine("-------------------foreach版本---------------------------");
            //foreach (IQuackable duck in ducks)
            //{
            //    duck.Quack();
            //}

            ////.net迭代器版本
            //Console.WriteLine("-------------------.net迭代器版本---------------------------");
            //IEnumerator<IQuackable> iterator = ducks.GetEnumerator();
            //while (iterator.MoveNext())
            //{
            //    IQuackable duck = iterator.Current;
            //    duck.Quack();
            //}

            //自己的迭代器版本
            Console.WriteLine("-------------------自己的迭代器版本---------------------------");
            Iterator<IQuackable> myiterator = CreateIterator();
            while (myiterator.HasNext())
            {
                IQuackable duck = myiterator.Next();
                duck.Quack();
            }
        }

 

        #endregion

        protected Iterator<IQuackable> CreateIterator()
        {
            return new DuckyIterator<IQuackable>(ducks);
        }

        #region IObservable Members

        public void Register(Observer.IObserver anObserver)
        {
            foreach (IQuackable duck in ducks)
            {
                duck.Register(anObserver);
            }
        }

        public void UnRegister(Observer.IObserver anObserver)
        {
            foreach (IQuackable duck in ducks)
            {
                duck.UnRegister(anObserver);
            }
        }

        public void NotifyObservers(object anObject)
        {
        }

        #endregion
    }
}
